package com.shinewind.search.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.pojo.doc.ArticleDoc;
import com.shinewind.pojo.dto.PageDto;
import com.shinewind.pojo.entity.Article;
import com.shinewind.pojo.entity.Search;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.ArticleVo;
import com.shinewind.pojo.vo.PageVo;
import com.shinewind.pojo.vo.UserSaveVo;
import com.shinewind.search.service.IArticleService;
import com.shinewind.search.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/index")
@Api(value = "首页搜索接口", description = "用于实现搜索热度和搜索功能")
public class SearchController {
    @Autowired
    private ISearchService iSearchService;

    @GetMapping("/search/hot/list")
    @ApiOperation(value = "搜索框<热度>下拉列表", notes = "焦点到搜索框上时，返回的列表", response = Result.class)
    public Result getHotList() {
        List<Search> ModelList = iSearchService.getRandomHotList();
        if (!ModelList.isEmpty()) {
            List<String> hotList = ModelList.stream().map(search -> {
                if (search != null && search.getTitle() != "") {
                    return search.getTitle();
                }
                return null;
            }).collect(Collectors.toList());
            return Result.success(hotList, BooleanUtil.isTrue(hotList.size()>0), messageConstant.QUERY_SUCCESS);
        }
        return Result.fail(false,messageConstant.QUERY_ERROR);
    }

    @PostMapping("/search/keyword")
    @ApiOperation(value = "搜索功能", notes = "搜索功能", response = PageVo.class)
    public PageVo<ArticleDoc> selectArticleByKeyword(@RequestBody(required = false) PageDto pageDto,
                                                    @RequestParam("keyword") String keyword) throws IOException {
        System.out.println("pageDto"+pageDto);
        System.out.println("keyword"+keyword);
        PageVo<ArticleDoc> articleVoPageVo = iSearchService.getArticleByKeyword(pageDto,keyword);
        return articleVoPageVo;
    }
}
